package com.yt.fras.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yt.fras.pojo.base.UserInputExcel;
import com.yt.fras.pojo.entity.GroupUser;
import com.yt.fras.pojo.entity.ManagementGroup;
import com.yt.fras.pojo.entity.User;
import com.yt.fras.pojo.entity.UserRole;
import com.yt.fras.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * ClassName:ExcelServiceImpl
 * Package:com.yt.fras.service.impl
 * Description:
 *
 * @date:2021/11/15 0:08
 * @author:yt
 */
@Service
public class ExcelServiceImpl implements ExcelService {

    @Autowired
    private ManagementGroupService managementGroupService;

    @Autowired
    private GroupUserService groupUserService;

    @Autowired
    private UserService userService;

    @Autowired
    private ThreadPoolTaskExecutor applicationTaskExecutor;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private UserRoleService userRoleService;

    @Override
    public void addGroupUsers(List<UserInputExcel> userInputExcels, String groupId) {
        System.out.println("groupId = " + groupId);
        userInputExcels.forEach(userExcel -> System.out.println(userExcel));
        List<GroupUser> groupUsers = new ArrayList<>();
        ManagementGroup managementGroup = managementGroupService.getById(groupId);

        QueryWrapper<GroupUser> wrapper = new QueryWrapper<>();
        final int[] count = {0};
        CompletableFuture<Void> groupUsersFuture = CompletableFuture.runAsync(() -> {
            for (UserInputExcel userExcel : userInputExcels) {

                User user = userService.queryByUsername(userExcel.getUsername());
                if (user != null) {
                    if (!managementGroup.getAdminId().equals(user.getId())) {
                        wrapper.eq("user_id", user.getId())
                                .eq("group_id", managementGroup.getId());
                        if (groupUserService.getOne(wrapper) == null) {
                            GroupUser groupUser = new GroupUser();
                            groupUser.setGroupId(managementGroup.getId());
                            groupUser.setUserId(user.getId());
                            count[0]++;
                            groupUsers.add(groupUser);
                        }
                    }
                } else {
                    User newUser = new User();
                    newUser.setUsername(userExcel.getUsername());
                    newUser.setPassword(passwordEncoder.encode("123456"));
                    if (userService.save(newUser)) {
                        userRoleService.save(new UserRole(newUser.getId(), 3));
                        GroupUser groupUser = new GroupUser();
                        groupUser.setGroupId(managementGroup.getId());
                        groupUser.setUserId(newUser.getId());
                        count[0]++;
                        groupUsers.add(groupUser);
                    }
                }
            }
        }, applicationTaskExecutor);
        try {
            CompletableFuture.allOf(groupUsersFuture).get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        groupUserService.saveBatch(groupUsers);
        managementGroup.setCount(managementGroup.getCount() + count[0]);
        managementGroupService.updateById(managementGroup);
    }

}
